package com.example.myrecipes;

import java.util.ArrayList;
import java.util.HashSet;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RecipeActivity extends Activity {
	ArrayAdapter<String> instructionsAdapter;
	ArrayAdapter<String> ingredientsAdapter;
	HashSet<String> ingredients;
	ArrayList<String> instructions;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupCustomOptionMenu();
		
		setContentView(R.layout.activity_recipe);
	
		Intent intent = getIntent();
		Recipe recipe = intent.getExtras().getParcelable("Recipe");
		String recipeName = recipe.getRecipeName();
		String yield = recipe.getYield();
		String prepTime = recipe.getPrepTime();
		String cookTime = recipe.getCookTime();
		String readyInTime = recipe.getReadyInTime();

		ingredients = recipe.getIngredients();
		instructions = recipe.getInstructions();

		ArrayList<String> ingredientsList = new ArrayList<String>(ingredients);

		TextView textViewRecipeName = (TextView) findViewById(R.id.recipe_name);
		textViewRecipeName.setText(recipeName);

		TextView textViewYieldAmount = (TextView) findViewById(R.id.yieldAmount);
		textViewYieldAmount.setText(yield);
		
		TextView textViewPrepTime = (TextView) findViewById(R.id.prepTime);
		textViewPrepTime.setText(prepTime);
		
		TextView textViewCookTime = (TextView) findViewById(R.id.cookTime);
		textViewCookTime.setText(cookTime);
		
		TextView textViewReadyInTime = (TextView) findViewById(R.id.readyInTime);
		textViewReadyInTime.setText(readyInTime);
		
		ListView ingredientsView = (ListView) findViewById(R.id.list_ingredients);
		ListView instructionsView = (ListView) findViewById(R.id.list_instructions);

		ingredientsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ingredientsList);
		ingredientsView.setAdapter(ingredientsAdapter);

		instructionsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, instructions);
		instructionsView.setAdapter(instructionsAdapter);
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private void setupCustomOptionMenu() {
		LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
		final View customActionBarView = inflater.inflate(R.layout.view_recipe_menu, null);
		customActionBarView.findViewById(R.id.actionbar_back).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		setupActionBar(customActionBarView);
	}
	
	private void setupActionBar(View customActionBarView) {
		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(
				ActionBar.DISPLAY_SHOW_CUSTOM,
				ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
				| ActionBar.DISPLAY_SHOW_TITLE);
		actionBar.setCustomView(customActionBarView, new ActionBar.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
	}
}
